package com.sdry.product.prod.controller;
import	java.io.UnsupportedEncodingException;

import com.google.gson.Gson;
import com.sdry.basic.entity.Material;
import com.sdry.common.BaseAPIMessageTypeEnum;
import com.sdry.common.BaseController;
import com.sdry.common.ResultAppInfo;
import com.sdry.exception.BizException;
import com.sdry.product.prod.entity.ResultAppList;
import com.sdry.product.prod.service.ProductTwoPaintService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description app端二遍漆
 * @Author chenBin
 * @Date 2020-04-26 10:57
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/productTwoPaintApp")
public class ProductTwoPaintAppController extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private ProductTwoPaintService productTwoPaintService;

    /**
     * 扫码入二遍漆
     * [
     *        {
     * 		"psnumber": "20-11",
     * 		"psId": "21",
     * 		"list": [
     *                    {
     * 					"maId": "15",
     * 					"quantity": "298",
     * 					"maSprayingRequirements": "银色"
     *                    }
     * 				]
     *    }
     * ]
     * @param json 数据
     * @param userId 用户ID
     * @return ResultAppInfo
     */
    @RequestMapping(value = "/api/addProductTwoPaintApp")
    public ResultAppInfo addProductTwoPaintApp(String json, String userId) {
        try {
            productTwoPaintService.addProductTwoPaintApp(json,userId);
            return getResultAppInfo("1",null,"");
        } catch (BizException e) {
            return getResultAppInfo("0",null, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return getResultAppInfo("0",null, BaseAPIMessageTypeEnum.A001.getName());
        }
    }

    /**
     * 查询二遍漆物料
     * @param psnumber 编号
     * @return ResultAppInfo
     */
    @RequestMapping(value = "/api/queryProductTwoPaintMateriel")
    public ResultAppInfo queryProductTwoPaintMateriel(String psnumber) {
        logger.info("查询二遍漆物料 psnumber = " +psnumber);
        Gson gson = new Gson();
        String psNumber = gson.fromJson(psnumber, String.class);
        List<ResultAppList<Material>> resultAppListList = productTwoPaintService.queryProductTwoPaintMateriel(psNumber);
        return getResultAppInfo("1",resultAppListList,"");
    }

    @RequestMapping(value = "/api/updateProductTwoPaintApp")
    public ResultAppInfo updateProductTwoPaintApp(String json, String userId) {
        try {
            productTwoPaintService.updateProductTwoPaintApp(json,userId);
            return getResultAppInfo("1",null,"");
        } catch (Exception e) {
            e.printStackTrace();
            return getResultAppInfo("0",null, BaseAPIMessageTypeEnum.A001.getName());
        }
    }

}
